import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { ConversationType } from "../../../domain/im/entity";

export const UseIsGroupUser = (conversation: any) => {


    const groupMember = useSelector((state: any) => state.groupMember.value);

    const user = useSelector((state: any) => state.user.value.user);

    const [showInput, setShowInput] = useState(true);

    useEffect(() => {
        if (conversation) {
            if (conversation.type == ConversationType.group) {        
                const array = groupMember[`${conversation.targetId}`];
                if (array) {
                    const index = array.findIndex((item: any) => item.userId == user.userId);
                    if (index == -1) {
                        setShowInput(false);
                    } else {
                        setShowInput(true);
                    }
                } else {
                    setShowInput(true);
                }
            } else {
                setShowInput(true);
            }
        }
    }, [conversation,groupMember])


    return { showInput };


}